import { request } from "@/src/service";
import Taro from "@tarojs/taro";

const customer_order_pay = (data) =>
  request({
    api: "customer.order.pay",
    data,
    flag: "customer-wechat",
  });

const onPay = async (order_id: number, cb, onCancel?: any) => {
  Taro.showLoading({ mask: true, title: "" });
  const { result, status } = await customer_order_pay({ order_id });

  if (status === "ok") {
    const params = result.pay_info;

    Taro.requestPayment({
      timeStamp: params.timestamp,
      nonceStr: params.noncestr,
      package: params.package,
      paySign: params.sign,
      signType: "MD5",
      success(res) {
        console.log("支付结果--->>>", res);
        Taro.hideLoading();
        if (typeof cb === "function") {
          cb();
        }
      },
      fail(res) {
        console.log("支付失败结果--->>>", res);
        Taro.hideLoading();
        if (typeof onCancel === "function") {
          onCancel();
        }
      },
    });
  } else {
    Taro.hideLoading();
  }
};

export default onPay;
